/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.cms.site.service;

import com.jeeplus.cms.util.CmsCacheUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.cms.column.entity.CmsColumn;
import com.jeeplus.modules.cms.column.service.CmsColumnService;
import com.jeeplus.modules.cms.link.entity.CmsLink;
import com.jeeplus.modules.cms.link.service.CmsLinkService;
import com.jeeplus.modules.cms.model.entity.CmsModel;
import com.jeeplus.modules.cms.model.service.CmsModelService;
import com.jeeplus.modules.cms.site.entity.CmsSite;
import com.jeeplus.modules.cms.site.mapper.CmsSiteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 网站Service
 *
 * @author Sawyer
 * @version 2020-11-21
 */
@Service
@Transactional(readOnly = true)
public class CmsSiteService extends CrudService<CmsSiteMapper, CmsSite> {

    @Autowired
    private CmsModelService modelService;

    @Autowired
    private CmsColumnService columnService;

    @Autowired
    private CmsLinkService linkService;

    @Override
    public CmsSite get(String id) {
        return super.get(id);
    }

    @Override
    public List<CmsSite> findList(CmsSite cmsSite) {
        return super.findList(cmsSite);
    }

    @Override
    public List<CmsSite> findAllList(CmsSite entity) {
        return findList(entity);
    }

    @Override
    public Page<CmsSite> findPage(Page<CmsSite> page, CmsSite cmsSite) {
        return super.findPage(page, cmsSite);
    }

    @Transactional(readOnly = false)
    @Override
    public void save(CmsSite cmsSite) {
        // 新增记录时初始化数据
        if (null == cmsSite.getId()) {
            cmsSite.init();
        }
        cmsSite.setLatitude(org.apache.commons.lang3.StringUtils.defaultIfBlank(cmsSite.getLatitude(), "0"));
        cmsSite.setLongitude(org.apache.commons.lang3.StringUtils.defaultIfBlank(cmsSite.getLongitude(), "0"));
        super.save(cmsSite);
        // 清除缓存
        CmsCacheUtils.removeCache(CmsCacheUtils.SITE_LIST_CACHE);
    }

    @Transactional(readOnly = false)
    @Override
    public void delete(CmsSite cmsSite) {
        // 查找当前site是否被关联
        CmsColumn cmsColumn = new CmsColumn();
        cmsColumn.setSiteId(cmsSite.getId());
        List<CmsColumn> columnList = columnService.findList(cmsColumn);
        if (null != columnList && columnList.size() > 0) {
            throw new RuntimeException(String.format("站点[%s]已被栏目[%s]使用", cmsSite.getId(), columnList.get(0).getName()));
        }

        CmsModel cmsModel = new CmsModel();
        cmsModel.setSiteId(cmsSite.getId());
        List<CmsModel> modelList = modelService.findList(cmsModel);
        if (null != modelList && modelList.size() > 0) {
            throw new RuntimeException(String.format("站点[%s]已被模型[%s]使用", cmsSite.getId(), modelList.get(0).getName()));
        }

        CmsLink cmsLink = new CmsLink();
        cmsLink.setSiteId(cmsSite.getId());
        List<CmsLink> linkList = linkService.findList(cmsLink);
        if (null != linkList && linkList.size() > 0) {
            throw new RuntimeException(String.format("站点[%s]已被链接[%s]使用", cmsSite.getId(), linkList.get(0).getName()));
        }


        super.delete(cmsSite);
        // 清除缓存
        CmsCacheUtils.removeCache(CmsCacheUtils.SITE_LIST_CACHE);
    }

    /**
     * 获取站点的首页点击数
     *
     * @param siteId 站点id
     * @return 首页点击数
     */
    @Transactional(readOnly = false)
    public int getHomeHits(final String siteId) {
        if (org.apache.commons.lang3.StringUtils.isBlank(siteId)) {
            return 0;
        }
        CmsSite site = super.get(siteId);
        if (null == site) {
            return 0;
        }
        return null == site.getHomeHits() ? 0 : site.getHomeHits();
    }

    @Transactional(readOnly = false)
    public void updateHitsAddOne(String id) {
        mapper.updateHitsAddOne(id);
        CmsCacheUtils.getSiteListByCache(false)
                .stream()
                .filter(content -> id.equals(content.getId()))
                .findFirst().ifPresent(content -> {
            content.setHomeHits(null == content.getHomeHits() ? 1 : content.getHomeHits() + 1);
        });
    }
}
